package cn.edu.sdjzu.xg.xkgl.controller.eduadmin;

import cn.edu.sdjzu.xg.xkgl.domain.CourseSelection;
import cn.edu.sdjzu.xg.xkgl.service.CourseSelectionService;
import util.Helper;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/exitCourseAcodingStuUsernameController")
public class ExitCourseAcodingStuUsernameController extends HttpServlet{
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //设置请求参数
        try {
            if("detail".equals(request.getParameter("action"))){
                int courseSelectionId = Helper.getIdFromRequest(request,"courseSelectionId");
                CourseSelection  selection = CourseSelectionService.getCourseSelectionService().find(courseSelectionId);
                CourseSelectionService.getCourseSelectionService().delete(courseSelectionId);

                response.sendRedirect("/courseDetailController?id="+selection.getCourse().getId());
            }else{
                int courseSelectionId = Helper.getIdFromRequest(request,"courseSelectionId");
                CourseSelectionService.getCourseSelectionService().delete(courseSelectionId);
                //请求重定向到指定路径
                request.getRequestDispatcher("/pages/eduadmin/selection/ExitCourse.jsp").forward(request,response);
            }
        } catch (Exception e) {
            //将message属性值存储到request对象中
            request.setAttribute("message",e.getMessage());
            //请求转发到指定路径
            request.getRequestDispatcher("/pages/error.jsp").forward(request,response);
        }

    }
}
